/*     */ package com.zimbra.cs.util.tnef.mapi;
/*     */ 
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import java.io.IOException;
/*     */ import java.text.ParseException;
/*     */ import java.util.Date;
/*     */ import java.util.GregorianCalendar;
/*     */ import java.util.TimeZone;
/*     */ import net.fortuna.ical4j.model.DateTime;
/*     */ import net.fortuna.ical4j.model.UtcOffset;
/*     */ import net.fortuna.ical4j.model.property.DtStart;
/*     */ import net.fortuna.ical4j.model.property.RRule;
/*     */ import net.freeutils.tnef.RawInputStream;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class TZRule
/*     */ {
/*  37 */   static Log sLog = ZimbraLog.tnef;
/*     */   private boolean isEffectiveRule;
/*     */   private boolean isAssociatedWithRecurrence;
/*     */   private int StartYear;
/*     */   private int Bias;
/*     */   private int StandardBias;
/*     */   private int DaylightBias;
/*     */   private SYSTEMTIME StandardDate;
/*     */   private SYSTEMTIME DaylightDate;
/*     */   
/*     */   public TZRule(RawInputStream ris) throws IOException
/*     */   {
/*  49 */     int MajorVersion = ris.readU8();
/*  50 */     int MinorVersion = ris.readU8();
/*  51 */     int ReservedInt = ris.readU16();
/*     */     
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*  60 */     int TZRuleFlags = ris.readU16();
/*  61 */     this.isAssociatedWithRecurrence = ((TZRuleFlags & 0x1) == 1);
/*  62 */     this.isEffectiveRule = ((TZRuleFlags & 0x2) == 2);
/*  63 */     setStartYear(ris.readU16());
/*  64 */     ris.skip(14L);
/*  65 */     setBias(readI32(ris));
/*  66 */     setStandardBias(readI32(ris));
/*  67 */     setDaylightBias(readI32(ris));
/*  68 */     setStandardDate(new SYSTEMTIME(ris));
/*  69 */     setDaylightDate(new SYSTEMTIME(ris));
/*     */     
/*  71 */     if (sLog.isDebugEnabled()) {
/*  72 */       StringBuffer debugInfo = new StringBuffer();
/*  73 */       debugInfo.append("TZRule: effective=");
/*  74 */       debugInfo.append(this.isEffectiveRule);
/*  75 */       debugInfo.append(" AssociatedWithRecurrence=");
/*  76 */       debugInfo.append(this.isAssociatedWithRecurrence);
/*  77 */       debugInfo.append("\n");
/*  78 */       if (MajorVersion != 2) {
/*  79 */         debugInfo.append("    Unexpected MajorVersion=");
/*  80 */         debugInfo.append(MajorVersion);
/*  81 */         debugInfo.append("\n");
/*     */       }
/*  83 */       if (MinorVersion != 1) {
/*  84 */         debugInfo.append("    Unexpected MinorVersion=");
/*  85 */         debugInfo.append(MinorVersion);
/*  86 */         debugInfo.append("\n");
/*     */       }
/*  88 */       if (ReservedInt != 62) {
/*  89 */         debugInfo.append("    Unexpected Reserved=0x");
/*  90 */         debugInfo.append(Integer.toHexString(ReservedInt));
/*  91 */         debugInfo.append("\n");
/*     */       }
/*  93 */       debugInfo.append("    Start Year=");
/*  94 */       debugInfo.append(getStartYear());
/*  95 */       debugInfo.append(" Bias=");
/*  96 */       debugInfo.append(this.Bias);
/*  97 */       debugInfo.append(" StandardBias=");
/*  98 */       debugInfo.append(getStandardBias());
/*  99 */       debugInfo.append(" DaylightBias=");
/* 100 */       debugInfo.append(getDaylightBias());
/* 101 */       debugInfo.append("\n");
/* 102 */       debugInfo.append("    standard info:");
/* 103 */       if (getStandardDate() != null) {
/* 104 */         debugInfo.append(getStandardDate());
/* 105 */         debugInfo.append("\n");
/*     */       }
/* 107 */       if (getDaylightDate() != null) {
/* 108 */         debugInfo.append("    daylight info:");
/* 109 */         debugInfo.append(getDaylightDate());
/*     */       }
/* 111 */       sLog.debug(debugInfo);
/*     */     }
/*     */   }
/*     */   
/*     */   public TZRule() throws IOException {
/* 116 */     this.isAssociatedWithRecurrence = true;
/* 117 */     this.isEffectiveRule = true;
/* 118 */     setStartYear(1601);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public RRule icalStandardRRule()
/*     */   {
/* 126 */     RRule theRule = null;
/*     */     try {
/* 128 */       theRule = new RRule(null, icalStandardRRuleString());
/*     */     } catch (ParseException e) {
/* 130 */       if (sLog.isDebugEnabled()) {
/* 131 */         sLog.debug("Parse problem processing STANDARD rule", e);
/*     */       }
/* 133 */       theRule = null;
/*     */     }
/* 135 */     return theRule;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public RRule icalDaylightRRule()
/*     */   {
/* 144 */     RRule theRule = null;
/*     */     try {
/* 146 */       theRule = new RRule(null, icalDaylightRRuleString());
/*     */     } catch (ParseException e) {
/* 148 */       if (sLog.isDebugEnabled()) {
/* 149 */         sLog.debug("Parse problem processing DAYLIGHT rule", e);
/*     */       }
/* 151 */       theRule = null;
/*     */     }
/* 153 */     return theRule;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private String icalStandardRRuleString()
/*     */   {
/* 162 */     if (!hasDaylightSaving()) {
/* 163 */       return null;
/*     */     }
/*     */     
/*     */ 
/*     */ 
/*     */ 
/* 169 */     StringBuffer rrule = new StringBuffer("FREQ=YEARLY;BYDAY=");
/* 170 */     DayOfWeek dow = getStandardDate().getDayOfWeek();
/* 171 */     int occurNum = getStandardDate().getDay();
/* 172 */     rrule.append(rruleDayInfo(dow, occurNum));
/* 173 */     rrule.append(";BYMONTH=");
/* 174 */     rrule.append(getStandardDate().getMonth());
/* 175 */     return rrule.toString();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private String icalDaylightRRuleString()
/*     */   {
/* 184 */     if (!hasDaylightSaving()) {
/* 185 */       return null;
/*     */     }
/* 187 */     StringBuffer rrule = new StringBuffer("FREQ=YEARLY;BYDAY=");
/* 188 */     DayOfWeek dow = getDaylightDate().getDayOfWeek();
/* 189 */     int occurNum = getDaylightDate().getDay();
/* 190 */     rrule.append(rruleDayInfo(dow, occurNum));
/* 191 */     rrule.append(";BYMONTH=");
/* 192 */     rrule.append(getDaylightDate().getMonth());
/* 193 */     return rrule.toString();
/*     */   }
/*     */   
/*     */   public boolean daylightStartsOnFinalDowInMonth() {
/* 197 */     return getDaylightDate().getDay() == 5;
/*     */   }
/*     */   
/*     */   public boolean standardStartsOnFinalDowInMonth() {
/* 201 */     return getStandardDate().getDay() == 5;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private String rruleDayInfo(DayOfWeek dow, int occurNum)
/*     */   {
/* 211 */     StringBuffer dayInfo = new StringBuffer();
/* 212 */     if (occurNum == 5)
/* 213 */       occurNum = -1;
/* 214 */     dayInfo.append(occurNum);
/* 215 */     dayInfo.append(dow);
/* 216 */     return dayInfo.toString();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private Long readI32(RawInputStream ris)
/*     */     throws IOException
/*     */   {
/* 226 */     return Long.valueOf((ris.readU8() | ris.readU8() << 8 | ris.readU8() << 16 | ris.readU8() << 24) & 0xFFFFFFFFFFFFFFFF);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public boolean isEffectiveRule()
/*     */   {
/* 233 */     return this.isEffectiveRule;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public boolean isAssociatedWithRecurrence()
/*     */   {
/* 240 */     return this.isAssociatedWithRecurrence;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void setStartYear(int startYear)
/*     */   {
/* 247 */     this.StartYear = startYear;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public int getStartYear()
/*     */   {
/* 254 */     return this.StartYear;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void setBias(Long bias)
/*     */   {
/* 261 */     this.Bias = bias.intValue();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void setBias(int bias)
/*     */   {
/* 268 */     this.Bias = bias;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public int getBias()
/*     */   {
/* 275 */     return this.Bias;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void setStandardBias(Long standardBias)
/*     */   {
/* 282 */     this.StandardBias = standardBias.intValue();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public void setStandardBias(int standardBias)
/*     */   {
/* 290 */     this.StandardBias = standardBias;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public int getStandardBias()
/*     */   {
/* 297 */     return this.StandardBias;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void setDaylightBias(Long daylightBias)
/*     */   {
/* 304 */     this.DaylightBias = daylightBias.intValue();
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void setDaylightBias(int daylightBias)
/*     */   {
/* 311 */     this.DaylightBias = daylightBias;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public int getDaylightBias()
/*     */   {
/* 318 */     return this.DaylightBias;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void setStandardDate(SYSTEMTIME standardDate)
/*     */   {
/* 325 */     this.StandardDate = standardDate;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public SYSTEMTIME getStandardDate()
/*     */   {
/* 332 */     return this.StandardDate;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public void setDaylightDate(SYSTEMTIME daylightDate)
/*     */   {
/* 339 */     this.DaylightDate = daylightDate;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public SYSTEMTIME getDaylightDate()
/*     */   {
/* 346 */     return this.DaylightDate;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public int getStandardUtcOffsetMillis()
/*     */   {
/* 354 */     int offset = -1 * ((getBias() + getStandardBias()) * 60 * 1000);
/* 355 */     return offset;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public int getDaylightUtcOffsetMillis()
/*     */   {
/* 363 */     int offset = -1 * ((getBias() + getDaylightBias()) * 60 * 1000);
/* 364 */     return offset;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public UtcOffset getStandardUtcOffset()
/*     */   {
/* 372 */     long offset = -1 * ((getBias() + getStandardBias()) * 60 * 1000);
/* 373 */     return new UtcOffset(offset);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */   public UtcOffset getDaylightUtcOffset()
/*     */   {
/* 381 */     long offset = -1 * ((getBias() + getDaylightBias()) * 60 * 1000);
/* 382 */     return new UtcOffset(offset);
/*     */   }
/*     */   
/*     */   public boolean hasDaylightSaving() {
/* 386 */     if (getStandardDate().getMonth() == 0) {
/* 387 */       return false;
/*     */     }
/* 389 */     return true;
/*     */   }
/*     */   
/*     */   public DtStart getStandardDtStart()
/*     */   {
/* 394 */     return getDtStart(getStandardDate().getHour(), getStandardDate().getMinute());
/*     */   }
/*     */   
/*     */   public DtStart getDaylightDtStart()
/*     */   {
/* 399 */     return getDtStart(getDaylightDate().getHour(), getDaylightDate().getMinute());
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   private DtStart getDtStart(int hr, int min)
/*     */   {
/* 418 */     TimeZone utcZone = TimeZone.getTimeZone("UTC");
/* 419 */     GregorianCalendar gc = new GregorianCalendar(1601, 0, 1, hr, min);
/* 420 */     gc.setTimeZone(utcZone);
/* 421 */     Date startDate = gc.getTime();
/* 422 */     DateTime startDateTime = new DateTime(startDate);
/* 423 */     return new DtStart(startDateTime);
/*     */   }
/*     */   
/*     */   public boolean equivalentRule(TZRule other) {
/* 427 */     if (other == null) {
/* 428 */       return false;
/*     */     }
/* 430 */     if (this.Bias != other.getBias()) {
/* 431 */       return false;
/*     */     }
/* 433 */     if (this.StandardBias != other.getStandardBias()) {
/* 434 */       return false;
/*     */     }
/* 436 */     if (this.DaylightBias != other.getDaylightBias()) {
/* 437 */       return false;
/*     */     }
/* 439 */     if (!getStandardDate().equivalentInTimeZones(other.getStandardDate())) {
/* 440 */       return false;
/*     */     }
/* 442 */     if (!getDaylightDate().equivalentInTimeZones(other.getDaylightDate())) {
/* 443 */       return false;
/*     */     }
/* 445 */     return true;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/util/tnef/mapi/TZRule.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */